package com.entfrm.biz.it.manager;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.entfrm.biz.it.entity.Property;
import com.entfrm.biz.it.entity.PropertyRecord;
import com.entfrm.biz.it.service.PropertyRecordService;
import com.entfrm.biz.it.service.PropertyService;
import com.entfrm.core.base.constant.CommonConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * <h3>entfrm</h3>
 * <p>资产使用记录</p>
 *
 * @author : kyc
 * @date : 2020-08-08 13:32
 **/
@Component
public class PropertyRecordManager {

    @Autowired
    private PropertyRecordService propertyRecordService;

    @Autowired
    private PropertyService propertyService;

    public List<PropertyRecord> getPropertyRecords(Integer propertyId) {
        QueryWrapper<PropertyRecord> qw = new QueryWrapper<>();
        qw.lambda().eq(PropertyRecord::getPropertyId, propertyId).orderByDesc(PropertyRecord::getCreateTime);
        return propertyRecordService.list(qw);
    }

    /**
     * 新增使用记录
     */
    public void savePropertyRecord(PropertyRecord propertyRecord) {
        // 修改资产状态
        Property property = new Property();
        property.setId(propertyRecord.getPropertyId())
                .setStatus(CommonConstants.PropertyStatus.IN_USE);
        propertyService.updateById(property);
        // 新增记录
        propertyRecord.setUseStatus(CommonConstants.Status.YES);
        propertyRecordService.save(propertyRecord);
    }

    /**
     * 修改使用记录
     */
    public void updatePropertyRecord(PropertyRecord propertyRecord) {
        // 修改资产状态
        Property property = new Property();
        property.setId(propertyRecord.getPropertyId())
                .setStatus(CommonConstants.PropertyStatus.IN_STOCK);
        propertyService.updateById(property);
        // 修改记录
        propertyRecord.setUseStatus(CommonConstants.Status.NO);
        propertyRecordService.updateById(propertyRecord);
    }
}
